# -*- coding: utf-8 -*-
import os
from playwright.sync_api import Playwright, sync_playwright, expect

##添加到环境变量里
def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto('http://192.168.7.102/users/sign_in')

    # Interact with login form
    page.get_by_label("Username or email").fill("yangxc@it.lianfa.cn")
    page.get_by_label("Password").fill("victal2422")
    page.get_by_role("button", name="Sign in").click()

    session_storage = page.evaluate("() => JSON.stringify(sessionStorage)")
    os.environ["SESSION_STORAGE"] = session_storage

    # Set session storage in a new context
    session_storage = os.environ["SESSION_STORAGE"]
    context.add_init_script("""(storage => {
      if (window.location.hostname === 'example.com') {
        const entries = JSON.parse(storage)
        for (const [key, value] of Object.entries(entries)) {
          window.sessionStorage.setItem(key, value)
        }
      }
    })('""" + session_storage + "')")


if __name__ == '__main__':

    with sync_playwright() as playwright:
        run(playwright)
